package db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Item;
import model.Order;

/**
 *
 * @author Mads Nielsen <k4emic@gmail.com>
 */
public class ItemDAO extends ObjectDAO {

    public ItemDAO() {
    }

    @Override
    public boolean insert(Object object) {
        Item item = (Item) object;

        String sql = "INSERT INTO Item (item_no, name, price, group_id, min_stock) "
                + "VALUES(?, ?, ?, ?, ?)";

        PreparedStatement pstmt = prepareStatement(sql);

        try {
            bindInsertStatementParams(pstmt, item);
            pstmt.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(ItemDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    /**
     * Binds parameter values for insert statement
     * 
     * @param stmt
     * @param item 
     */
    private void bindInsertStatementParams(PreparedStatement stmt, Item item) throws SQLException {
        stmt.setString(1, item.getItemNo());
        stmt.setString(2, item.getName());
        stmt.setDouble(3, item.getPrice());
        stmt.setInt(4, item.getGroup().getId());
        stmt.setInt(5, item.getMinStock());
    }

    @Override
    public boolean update(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Object object) {

        Item item = (Item) object;
        String sql = "DELETE FROM Item WHERE item_no =?";
        PreparedStatement pstmt = prepareStatement(sql);
        try {
            pstmt.setString(1, item.getItemNo());
            pstmt.execute();

            return true;

        } catch (SQLException ex) {
            Logger.getLogger(ItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    @Override
    public Item[] findAll(String where, Object... params) {

        String sql = "SELECT * FROM Item WHERE " + where;
        PreparedStatement pstmt = prepareStatement(sql);
        
        try {
            bindStatementParams(pstmt, params);
        } catch (SQLException ex) {
            Logger.getLogger(ItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            ResultSet results = pstmt.executeQuery();
            return unpackResultSet(results);

        } catch (SQLException ex) {
            Logger.getLogger(ItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    private Item[] unpackResultSet(ResultSet results) throws SQLException {

        ArrayList<Item> list = new ArrayList<Item>();

        while (results.next()) {

            Item item = new Item();

            item.setItemNo(results.getString("item_no"));
            item.setName(results.getString("name"));
            item.setPrice(results.getDouble("price"));
            list.add(item);

        }

        return list.toArray(new Item[list.size()]);
    }

    @Override
    public Item findOne(String where, Object... params) {
        String sql = "SELECT * FROM Item WHERE " + where;
        PreparedStatement pstmt = prepareStatement(sql);
        
        try {
            bindStatementParams(pstmt, params);
        } catch (SQLException ex) {
            Logger.getLogger(ItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            ResultSet results = pstmt.executeQuery();
            Item[] items = unpackResultSet(results);
            
            if(items.length <= 0) {
                return null;
            }
            
            return items[0];
        } catch (SQLException ex) {
            Logger.getLogger(ItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }

    @Override
    public Item findByPK(Object pk) {
       String sqlWhere = "item_no = ?";
       
       return findOne(sqlWhere, pk);
    }
}
